using System;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using Org.BouncyCastle.Crypto.Engines;
using UnityEngine;
using UnityEngine.UI;

public class PretreatmentPanel : MonoBehaviour
{
    [SerializeField] private BoundarySettingPanel boundary_xhi;
    [SerializeField] private BoundarySettingPanel boundary_xlo;
    [SerializeField] private BoundarySettingPanel boundary_yhi;
    [SerializeField] private BoundarySettingPanel boundary_ylo;
    [SerializeField] private BoundarySettingPanel boundary_zhi;
    [SerializeField] private BoundarySettingPanel boundary_zlo;
    /// <summary>
    /// 来流分子数密度
    /// </summary>
    [SerializeField] private InputField global_nrhoInput;
    /// <summary>
    /// 真实与模拟分子数比
    /// </summary>
    [SerializeField] private InputField global_fnumInput;
    
    /// <summary>
    /// 粒子与物面碰撞模型
    /// </summary>
    [SerializeField] private ToggleGroup surf_collide_type_ToggleGroup;
    [SerializeField] private Toggle diffuse_Toggle;
    [SerializeField] private Toggle mirror_Toggle;
    /// <summary>
    /// 漫反射状态时的 壁面温度
    /// </summary>
    [SerializeField] private InputField wall_temperature_Input;
    /// <summary>
    /// 漫反射状态时的 反射比例
    /// </summary>
    [SerializeField] private InputField reflectivity_Input;
    /// <summary>
    /// 粒子与粒子碰撞模型
    /// </summary>
    [SerializeField] private ToggleGroup collide_alpha_ToggleGroup;
    [SerializeField] private Toggle soft_Toggle;
    [SerializeField] private Toggle hard_Toggle;
    
    /// <summary>
    /// 混合物 NO2 CO2 O2
    /// </summary>
    [SerializeField] private Toggle n2_Toggle;
    /// <summary>
    /// 混合物 CO2
    /// </summary>
    [SerializeField] private Toggle co2_Toggle;
    /// <summary>
    /// 混合物 O2
    /// </summary>
    [SerializeField] private Toggle o2_Toggle;
    [SerializeField] private InputField n2_Input;
    [SerializeField] private InputField co2_Input;
    [SerializeField] private InputField o2_Input;
    
    /// <summary>
    /// 温度
    /// </summary>
    [SerializeField] private InputField temperature_Input;
    
    /// <summary>
    /// 速度 Vx
    /// </summary>
    [SerializeField] private InputField v_stream_x_Input;
    /// <summary>
    /// 速度 Vy
    /// </summary>
    [SerializeField] private InputField v_stream_y_Input;
    /// <summary>
    /// 速度 Vz
    /// </summary>
    [SerializeField] private InputField v_stream_z_Input;
    // Start is called before the first frame update
    private void Start()
    {
        n2_Toggle.onValueChanged.AddListener(OnN2ToggleChanged);
        co2_Toggle.onValueChanged.AddListener(OnCO2ToggleChanged);
        o2_Toggle.onValueChanged.AddListener(OnO2ToggleChanged);
        n2_Input.onValueChanged.AddListener(OnN2InputChanged);
        co2_Input.onValueChanged.AddListener(OnCO2InputChanged);
        o2_Input.onValueChanged.AddListener(OnO2InputChanged);
        
    }
  
    private void OnN2ToggleChanged(bool isOn)
    {
        n2_Input.gameObject.SetActive(isOn);
    }
    private void OnCO2ToggleChanged(bool isOn)
    {
        co2_Input.gameObject.SetActive(isOn);
    }
    private void OnO2ToggleChanged(bool isOn)
    {
        o2_Input.gameObject.SetActive(isOn);
    }
    
    private void OnN2InputChanged(string value)
    {
        Debug.Log("OnN2InputChanged : "+value);
    }
    private void OnCO2InputChanged(string value)
    {
        Debug.Log("OnCO2InputChanged : "+value);
    }
    private void OnO2InputChanged(string value)
    {
        Debug.Log("OnO2InputChanged : "+value);
    }
    /// <summary>
    /// 获取来流分子数密度值
    /// </summary>
    /// <returns></returns>
    public string GetGlobalNrho()
    {
        return global_nrhoInput.text;
    }
    /// <summary>
    /// 真实与模拟分子数比
    /// </summary>
    /// <returns></returns>
    public string GetGlobalFnum()
    {
        return global_fnumInput.text;
    }
    
    /// <summary>
    /// 粒子与物面碰撞模型 类型   "specular" 镜面反射 "diffuse" 漫反射
    /// </summary>
    /// <returns></returns>
    public string GetSurfCollideType()
    {
        if (diffuse_Toggle.isOn)
        {
            return "diffuse";
        }
        if(mirror_Toggle.isOn)
        {
            return "specular";
        }
        return null;
    }
    /// <summary>
    /// 漫反射状态时的 壁面温度
    /// </summary>
    /// <returns></returns>
    public string GetWallTemperature()
    {
        return wall_temperature_Input.text;
    }
    /// <summary>
    /// 漫反射状态时的 反射比例
    /// </summary>
    /// <returns></returns>
    public string GetReflectivity()
    {
        return reflectivity_Input.text;
    }
    
    /// <summary>
    /// 粒子与粒子碰撞模型
    /// </summary>
    /// <returns></returns>
    public string GetCollideAlpha()
    {
        if(soft_Toggle.isOn)
        {
            return "1.4";
        }
        if(hard_Toggle.isOn)
        {
            return "1";
        }

        return null;
    }
    /// <summary>
    /// 获取混合物
    /// </summary>
    /// <returns></returns>
    public string[] GetMixGas()
    {
        List<string> list = new List<string>();
        if (n2_Toggle.isOn)
        {
            list.Add("N2");
        }
        if (co2_Toggle.isOn)
        {
            list.Add("CO2");
        }
        if (o2_Toggle.isOn)
        {
            list.Add("O2");
        }
        return list.ToArray();
    }

    public string GetMixGasString()
    {
        Dictionary<string,string> dic = new Dictionary<string, string>();
        if (n2_Toggle.isOn)
        {
            if(string.IsNullOrEmpty(n2_Input.text))
                dic.Add("N2","0");
            else
                dic.Add("N2",n2_Input.text);
        }
        if (co2_Toggle.isOn)
        {
            if(string.IsNullOrEmpty(co2_Input.text))
                dic.Add("CO2","0");
            else
                dic.Add("CO2",co2_Input.text);
        }
        if (o2_Toggle.isOn)
        {
            if(string.IsNullOrEmpty(o2_Input.text))
                dic.Add("O2","0");
            else
                dic.Add("O2",o2_Input.text);
        }

        return JsonMapper.ToJson(dic);
    }
    public Dictionary<string, string> GetMixGasDic()
    {
        Dictionary<string,string> dic = new Dictionary<string, string>();
        if (n2_Toggle.isOn)
        {
            if(string.IsNullOrEmpty(n2_Input.text))
                dic.Add("N2","0");
            else
                dic.Add("N2",n2_Input.text);
        }
        if (co2_Toggle.isOn)
        {
            if(string.IsNullOrEmpty(co2_Input.text))
                dic.Add("CO2","0");
            else
                dic.Add("CO2",co2_Input.text);
        }
        if (o2_Toggle.isOn)
        {
            if(string.IsNullOrEmpty(o2_Input.text))
                dic.Add("O2","0");
            else
                dic.Add("O2",o2_Input.text);
        }
        return dic;
    }
    
    /// <summary>
    /// 获取stream vx
    /// </summary>
    /// <returns></returns>
    public string GetStreamVX()
    {
        return v_stream_x_Input.text;
    }
    /// <summary>
    /// 获取stream vy
    /// </summary>
    /// <returns></returns>
    public string GetStreamVY()
    {
        return v_stream_y_Input.text;
    }
    /// <summary>
    /// 获取stream vz
    /// </summary>
    /// <returns></returns>
    public string GetStreamVZ()
    {
        return v_stream_z_Input.text;
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
           string val = GetBoundaryXhi();
           Debug.Log("value : " + val);
        }
    }

    public string GetBoundaryXhi()
    {
        Debug.Log("BoundaryXhi = "+boundary_xhi.GetToggleName());
        return boundary_xhi.GetToggleName();
    }

    public string GetBoundaryXlo()
    {
        Debug.Log("GetBoundaryXlo = "+boundary_xlo.GetToggleName());
        return boundary_xlo.GetToggleName();
    }

    public string GetBoundaryYhi()
    {
        Debug.Log("GetBoundaryYhi = "+boundary_yhi.GetToggleName());
        return boundary_yhi.GetToggleName();
    }

    public string GetBoundaryYlo()
    {
        Debug.Log("GetBoundaryYlo = "+boundary_ylo.GetToggleName());
        return boundary_ylo.GetToggleName();
    }

    public string GetBoundaryZhi()
    {
        Debug.Log("GetBoundaryZhi = "+boundary_zhi.GetToggleName());
        return boundary_zhi.GetToggleName();
    }

    public string GetBoundaryZlo()
    {
        Debug.Log("GetBoundaryZlo = "+boundary_zlo.GetToggleName());
        return boundary_zlo.GetToggleName();
    }

    public string GetTemperature()
    {
        return temperature_Input.text;
    }
}
